﻿using Abp.Application.Navigation;
using Mt.Site.Core.Navigations;
using Mt.Site.Core.Navigations.Navigation;
using Mt.Site.Core.Navigations.Values;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mt.Site
{
    public static class IMenuStoreExtension
    {
        public static async Task<MenuFrontEndView> GetMenuFrontEndViewAsync(this IMenuStore store, long auxiliaryId)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetMenuFrontEndViewAsync(item);
        }

        public static async Task<List<MenuExtendedData>> GetImageTextAsync(this IMenuStore store, long auxiliaryId, string key)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetImageTextAsync(item.Id(), key);
        }
        public static async Task<MenuExtendedData> GetSingleImageTextAsync(this IMenuStore store, long auxiliaryId, string key)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetSingleImageTextAsync(item.Id(), key);
        }

        public static async Task<Dictionary<string, List<MenuExtendedData>>> GetImageTextIntoDictionaryAsync(this IMenuStore store, long auxiliaryId)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetImageTextIntoDictionaryAsync(item.Id());
        }

        public static async Task<List<MenuAndMenuExtendedData>> GetAllSonMenuDataAsync(this IMenuStore store
            , long auxiliaryId, Func<MenuItemDefinition, ContentMenuItemCustomData, bool> predicate = null)
        {
            try
            {
                MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
                if (!string.IsNullOrEmpty(item.DefaultDisplayName))
                {
                    return await store.GetAllSonMenuDataAsync(item, predicate);
                }
                return new List<MenuAndMenuExtendedData>();
            }
            catch
            {
                return new List<MenuAndMenuExtendedData>();
            }

        }                                                                                                                                                                                                                                                                                              

        public static async Task<MenuItemInfo> GetMenuItemInfoAsync(this IMenuStore store, long auxiliaryId)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetMenuItemInfoAsync(item);
        }

        public static async Task<List<MenuItemInfo>> GetMenuItemInfosByTreeAsync(this IMenuStore store, long auxiliaryId)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetMenuItemInfosByTreeAsync(item);
        }

        public static async Task<List<MenuItemInfo>> GetAllSonMenuItemInfoAsync(this IMenuStore store
            , long auxiliaryId, Func<MenuItemDefinition, ContentMenuItemCustomData, bool> predicate = null)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetAllSonMenuItemInfoAsync(item, predicate);
        }


        public static async Task<MenuItemInfo> GetFirstSonMenuItemInfoAsync(this IMenuStore store
            , long auxiliaryId, Func<MenuItemDefinition, ContentMenuItemCustomData, bool> predicate = null)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);

            return await store.GetFirstSonMenuItemInfoAsync(item, predicate);
        }
        public static async Task<MenuItemInfo> GetFirstSonNavigationMenuItemInfoAsync(this IMenuStore store
            , long auxiliaryId, Func<MenuItemDefinition, ContentMenuItemCustomData, bool> predicate = null)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            return await store.GetFirstSonNavigationMenuItemInfoAsync(item, predicate);
        }

        /// <summary>
        /// 获取所有下级栏目的ID，包括其自身的ID
        /// </summary>
        /// <param name="store"></param>
        /// <param name="auxiliaryId"></param>
        /// <param name="IsRecurrence">是否递归</param>
        /// <param name="predicate">过滤条件</param>
        /// <returns></returns>
        public static async Task<List<long>> GetAllSonMenuItemIdAndSelf(this IMenuStore store, long auxiliaryId, bool IsRecurrence = false
            , Func<MenuItemDefinition, ContentMenuItemCustomData, bool> predicate = null)
        {
            try
            {
                MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
                if (!string.IsNullOrEmpty(item.DefaultDisplayName))
                {
                    List<long> list = await store.GetAllSonMenuItemIdAsync(item, IsRecurrence, predicate);
                    list.Insert(0, auxiliaryId);
                    return list;
                }
                return new List<long>();
            }
            catch
            {
                return new List<long>();
            }
        }

        /// <summary>
        /// 获取所有下级导航栏目的ID，包括其自身的ID
        /// </summary>
        /// <param name="store"></param>
        /// <param name="auxiliaryId"></param>
        /// <param name="IsRecurrence">是否递归</param>
        /// <param name="predicate">过滤条件</param>
        /// <returns></returns>
        public static async Task<List<long>> GetAllSonNavigationMenuItemIdAndSelf(this IMenuStore store, long auxiliaryId, bool IsRecurrence = false
            , Func<MenuItemDefinition, ContentMenuItemCustomData, bool> predicate = null)
        {
            MenuIdTraceCacheItem item = await store.GetMenuIdTraceCacheItemAsync(auxiliaryId);
            List<long> list = await store.GetAllSonNavigationMenuItemIdAsync(item, IsRecurrence, predicate);
            list.Insert(0, auxiliaryId);
            return list;
        }

        #region 推荐栏目

        /// <summary>
        ///  获取某模块的所有首页推荐栏目的ID
        /// </summary>
        /// <param name="moduleId">模块ID</param>
        /// <returns></returns>
        public static async Task<List<long>> GetRecommendMenuIdForHomeAsync(
            this IMenuStore store, Guid moduleId)
        {
            return await store.GetMenuIdsByModuleIdAsync(moduleId, true, (item, customData) =>
            {
                return customData.HomeRecommendation;
            });
        }

        /// <summary>
        /// 获取某模块的所有频道页推荐栏目的ID
        /// </summary>
        /// <param name="moduleId">模块ID</param>
        /// <param name="item">频道页的id路径</param>
        /// <returns></returns>
        public static async Task<List<long>> GetRecommendMenuIdForChannelAsync(
            this IMenuStore store, Guid moduleId, MenuIdTraceCacheItem item)
        {
            return await store.GetMenuIdsByModuleIdAsync(moduleId, item, true, (item, customData) =>
            {
                return customData.ChannelRecommendation;
            });
        }

        #endregion
    }
}
